package gui;

import java.util.regex.Pattern;

/**
 *
 * @author Gabrielle DuBois
 * @author Sarah Wegner
 */
public class Validator {

    private static final String zipRegex = "[0-9]{5}-([0-9]{4})?";
    private static final String stateRegex = "[a-zA-Z]{2}";
    private static final String phoneRegex = "/([0-9]{3}/)[0-9]{3}-[0-9]{4}";
    private static final String dateRegex = "[0-9]{2}/[0-9]{2}/[0-9]{4}";
    //private static final String moneyRegex = "[0-9]*/.[0-9]{2}";
    private static final String moneyRegex2 = "(\\d){1,25}(\\.(\\d){1,2})";


    private Validator(){ }//constructor

    /**
     * Checks all rows in tableModel to ensure that required columns are not
     * empty strings.
     * @param tableModel
     * @param requiredColumns an array of ints specifying the column numbers in
     *                  the table model which need to be checked.
     * @return A boolean. Returns true to indicate all rows in required columns
     *          have been filled in and false otherwise.
     */
    public static boolean checkRequiredFields(TableModel tableModel, int[] requiredColumns){
        for(int i = 0; i < tableModel.getRowCount(); i++){
            //Check each row in tableModel
            for(int j = 0; j < requiredColumns.length; j++){
                //Check each required column number found in requiredColumns array
                if(tableModel.getValueAt(i,j).equals("")){
                    return false;
                }//if
            }//for(j)
        }//for(i)

        return true;
    }//checkRequiredFields()

    /**
     * Checks tableModel to ensure that every row in the zipdcode column
     * contains a valid zipcode in the form of: '00000' or '00000-0000'
     * @param tableModel
     * @param zipColumn
     * @return A boolean. Returns true to indicate all rows in zipcode column
     *          contain a valid zipcode in the form of: '00000' or '00000-0000'
     *          Returns false otherwise.
     */
    public static boolean verifyZipFields(TableModel tableModel, int zipColumn){
        for(int i = 0; i < tableModel.getRowCount(); i++){
            String zipField = (String)tableModel.getValueAt(i, zipColumn);

            if(!Pattern.matches(zipRegex, zipField)){
                return false;
            }//if
        }//for

        return true;
    }//verifyZipFields()

    /**
     * Checks tableModel to ensure that every row in the state column
     * contains a valid state in the form of: 'XX' or 'xx'
     * @param tableModel
     * @param zipColumn an int specifying the column number in the table model
     *                  which is the state column.
     * @return A boolean. Returns true to indicate all rows in state column
     *          contain a valid state in the form of: 'XX' or 'xx'
     *          Returns false otherwise.
     */
    public static boolean verifyStateFields(TableModel tableModel, int zipColumn){
        for(int i = 0; i < tableModel.getRowCount(); i++){
            String stateField = (String)tableModel.getValueAt(i, zipColumn);

            if(!Pattern.matches(stateRegex, stateField)){
                return false;
            }//if
        }//for

        return true;
    }//verifyStateFields()


//METHODS BELOW WILL BE DELETED
    public static boolean isState(String state){
        if(!Pattern.matches(stateRegex, state)){
                return false;
        }//if

        return true;
    }//isState()

    public static boolean isZipcode(String zip){
        if(!Pattern.matches(zipRegex, zip)){
                return false;
        }//if

        return true;
    }//isZipcode()

    public static boolean isPhoneNumber(String phone){
        if(!Pattern.matches(phoneRegex, phone)){
                return false;
        }//if

        return true;
    }//isPhoneNumber()

    public static boolean isDate(String phone){
        if(!Pattern.matches(dateRegex, phone)){
                return false;
        }//if

        return true;
    }//isDate()

    public static boolean isMoney(String money){
        if(!Pattern.matches(moneyRegex2, money) || money.equals("") || money.isEmpty()){
                return false;
        }//if

        return true;
    }//isMoney()

}//Validator
